package cn.momoshenchi.service;


import cn.momoshenchi.dao.StudentMapper;
import cn.momoshenchi.pojo.Student;

import cn.momoshenchi.util.AssertUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/3/30 - 15:20
 */
@Service
public class StudentService
{

    @Resource
    private StudentMapper studentMapper;

    public void add(Student student)
    {
        AssertUtil.isTrue( studentMapper.insert(student)!=1,"增加失败");
    }

    public void update(Student student)
    {
        AssertUtil.isTrue(studentMapper.updateByPrimaryKey(student)!=1,"更新失败");
    }
    public void delete(Integer id)
    {
        AssertUtil.isTrue(studentMapper.deleteByPrimaryKey(id)!=1,"删除失败");
    }

    public Map<String, Object> selectAll(int page,int size)
    {
        PageHelper.startPage(page,size);
        List<Student> list = studentMapper.selectAll();
        PageInfo<Student> pageInfo=new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data",list);
        map.put("totalItem",pageInfo.getTotal());
        map.put("currentPage",page);
        map.put("size",size);
        return map;
    }

    public Student selectById(Integer id)
    {
        Student student= studentMapper.selectByPrimaryKey(id);
        return student;
    }
}
